package com.atguigu.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Yanglinfengkk
 * 自定义配置ElasticSearch API整合SpringBoot
 *  1.导入依赖
 *  2.编写配置,给容器中注入一个RestHighLevelClient
 *  3.参照API(官方文档)
 *      https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html
 */
@Configuration
public class GuLiMallElasticSearchConfig {
    /**
     * 通用配置项
     */
    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        /*builder.addHeader("Authorization", "Bearer " + TOKEN);
        builder.setHttpAsyncResponseConsumerFactory(
                new HttpAsyncResponseConsumerFactory
                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));*/
        COMMON_OPTIONS = builder.build();
    }

    /**
     * 实例化一个RestHighLevelClient对象
     * @return
     */
    @Bean
    public RestHighLevelClient eSRestClient(){
        RestClientBuilder builder=null;
        //String hostname, int port, String scheme
        builder = RestClient.builder(new HttpHost("192.168.220.128", 9200, "http"));
        RestHighLevelClient client=new RestHighLevelClient(builder);

        /*RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.220.128", 9200, "http")));
        return client;*/
        return client;
    }
}
